package com.example.shardingdemo.service;

import com.example.shardingdemo.entity.Order;
import com.example.shardingdemo.mapper.OrderMapper;
import com.example.shardingdemo.util.SnowFlakeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Calendar;
import java.util.List;
import java.util.Random;


@Service
public class OrderService {

    @Autowired
    OrderMapper orderMapper;

    public Order getOrder(long id,String tableSuffix){
        return orderMapper.getOrder(id,tableSuffix);
    }

    public void saveOrder(String tableSuffix) {
        SnowFlakeUtil snowFlakeUtil = new SnowFlakeUtil();
        Random r = new Random();
        for (long i = 1; i < 11; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -r.nextInt(10));
            int i1 = r.nextInt(10) + 1;
            Order order =  new Order();
            //基因法生成uuid
            order.setUserId(i1);
            order.setUuid(snowFlakeUtil.generateWithGene(snowFlakeUtil.nextId(),  snowFlakeUtil.fetchGene((long) i1, 4)));
            order.setOrderDate(calendar.getTime());
            order.setAddress("地址"+i1);
            order.setUserName("jxq"+i1);
            order.setTableSuffix(tableSuffix);
            orderMapper.addOrder(order);
            System.out.println("插入订单成功，uid=" + order.getUuid());
        }
    }

    public PageInfo getOrderPage(int pageNum, int pageSize,String tableSuffix) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Order> page = orderMapper.queryOrderByPage(tableSuffix);
        return new PageInfo(page);
    }
    public List<Order> getOrderList(Order order) {
        return orderMapper.getOrderList(order);
    }

}
